package eos.dom;

import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class DomChooseGamePopup extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.choosegamepopuplayout);

		ListView gameList = (ListView)findViewById(R.id.ChoiceGameList);
		gameList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		Vector<String> list = new Vector<String>();
		try {
			list = DomStart.client.receiveGameList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayAdapter<Object> a = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_single_choice, list.toArray());
		gameList.setAdapter(a);

		Button join = (Button)findViewById(R.id.JoinButton);
		join.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ListView gameList = (ListView)findViewById(R.id.ChoiceGameList);
				if(gameList.getCheckedItemPosition() != ListView.INVALID_POSITION && !gameList.getAdapter().isEmpty()){
					String sel = gameList.getItemAtPosition(gameList.getCheckedItemPosition()).toString();
					sel = sel.substring(sel.indexOf("[")+1, sel.indexOf("]"));
					DomStart.gameToJoin = sel;
					setResult(DomStart.CHOICE_MADE);
					DomChooseGamePopup.this.finish();
				}
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		setResult(DomStart.CANCELLED);
		finish();
	}

}
